#include "Run.h"
#include <algorithm>
#include "DelphesData.h"
#include "d3pdrel16.h"
#include "d3pdrel15.h"
Run::Run(){
nevents=0;	
}

Run::~Run(){
	AnaMap.clear();
	CfgFileList.clear();
}

std::vector<std::string> Run::getCfgFileList(){
	return CfgFileList;
}
std::map<std::string,Analysis*> Run::getAnaMap(){
	return AnaMap;
}
void Run::ConfigureRun(std::string fname/*Run.cfg*/,std::string a_rootfile/**Your input rootfile containing your tree to analyse*/){
  
ConfigParser* prsr=new ConfigParser();

prsr->PaerseRunSettings(fname);//now AnaMap should have been allocated&filled properly
Run::CfgFileList=prsr->getCfgFileList();//get the CfgFileList from the parser
Run::AnaMap=prsr->getAnaMap();
Run::nevents=prsr->Nevents;
Run::RunName=prsr->RunName;
Run::DataType=prsr->DataType;
Run::doGRL=prsr->doGRL;
Run::TreeName=prsr->TreeName;
Run::rootfile=a_rootfile;
return;	
}

void Run::ConfigureAnalyses(){//configure analyses in the map from their corresponding configuration files
        //get the pointer to the tree first do not close the file
	for(unsigned int i=0;i<AnaMap.size();i++){
                TFile *f=TFile::Open(Run::rootfile.c_str(),"READ");
                TTree *atree=(TTree*)f->Get(Run::TreeName.c_str());
                AnaMap[CfgFileList[i]]->setTree(atree);
		AnaMap[CfgFileList[i]]->Configure(CfgFileList[i]);
		if(Run::DataType.compare("Delphes")==0){
		  AnaMap[CfgFileList[i]]->SetEvtData(new DelphesData(atree));
		  std::cout<<" Analysis: "<<AnaMap[CfgFileList[i]]->name<<" DataType: "<<Run::DataType<<std::endl;
		}
	    else if (Run::DataType.compare("d3pdrel16")==0){
		  AnaMap[CfgFileList[i]]->SetEvtData(new d3pdrel16(atree));
		  std::cout<<" Analysis: "<<AnaMap[CfgFileList[i]]->name<<" DataType: "<<Run::DataType<<std::endl;
		}
		
         else if (Run::DataType.compare("d3pdrel15")==0){
                  AnaMap[CfgFileList[i]]->SetEvtData(new d3pdrel15(atree));
                  std::cout<<" Analysis: "<<AnaMap[CfgFileList[i]]->name<<" DataType: "<<Run::DataType<<std::endl;
        }
                	
		else{
		  std::cout<<"DataType: "<<Run::DataType<<" is not in Allowed Data Types . Check Your configuration"<<std::endl;
		  std::cout<<"Allowed Data Types are : Delphes, d3pdrel15, d3pdrel16"<<std::endl;
		  exit(-8);
		}
	}
	return;	
}

void Run::RunAnalyses(){
	for(unsigned int i=0;i<AnaMap.size();i++){//loop over analyses from the map
		AnaMap[CfgFileList[i]]->Execute();
	}
return;
}
void Run::ClearEvtdata()
{
  for(unsigned int i=0;i<Run::AnaMap.size();i++){
    Run::AnaMap[Run::CfgFileList[i]]->GetEvtdata()->ClearEvtData();
  }
}

void Run::FillEvtData ( int ievt)
{
  int fillctr=0;
 for(unsigned int i=0;i<Run::AnaMap.size();i++){
/*   if(AnaMap[CfgFileList[i]]==NULL){
     std::cout<<"FATAL*** EvtData is NULL"<<std::endl;
     exit(-8);
  }
*/  
  Run::AnaMap[Run::CfgFileList[i]]->GetEvtdata()->FillEvtData(ievt);//bug burda
    fillctr++;
  }
}


void::Run::Next(int iEvt){
  //each time Next is called all evtdata of analyses shall be cleared and they are filled back with the next entry
  for(unsigned int i=0;i<AnaMap.size();i++){
    //std::cout<<"Run clear called"<<std::endl; 
    AnaMap[CfgFileList[i]]->GetEvtdata()->ClearEvtData();
  }
  
  //Fill
 
 for(unsigned int i=0;i<AnaMap.size();i++){
   //std::cout<<"RunFillevtda called"<<std::endl;
    AnaMap[CfgFileList[i]]->GetEvtdata()->FillEvtData(iEvt);
  }
  
}
void Run::Finalize(TString fname){//finalize all analyses in the map
	for(unsigned int i=0;i<AnaMap.size();i++){
			AnaMap[CfgFileList[i]]->Finalize(fname);
	}
	
return;	
}
